package com.crm.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.crm.pojo.Book;

public class BookDao {

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	public void save(Book book){
		getSession().saveOrUpdate(book);
	}
	public void del(Book book){
		getSession().delete(book);
	}
	public void del(int id){
		getSession().delete(findById(id));
	}
	public Book findById(int id){
		return (Book) getSession().get(Book.class,id);
	}
	
	@SuppressWarnings("unchecked")
	public List<Book> findAll(){
		Criteria c = getSession().createCriteria(Book.class);
		return c.list();
	}
}
